<script setup lang="ts" name="PanelDocument">
import { mapState } from 'pinia'
import { useModelerStore } from '@/store/bpmnProcess/modelerStore'
import { hasProcessRef, getDocumentValue, setDocumentValue, getProcessDocumentValue, setProcessDocumentValue } from '../../utils/document'
import EventEmitter from '@/utils/EventEmitter'
const form = ref({
  elementDoc: '',
  processDoc: ''
})
const isProcess = ref(false)
const mapStates = computed(() => {
  return mapState(useModelerStore, ['getActive'])
})

const updateElementDoc = (value: string) => {
  setDocumentValue(mapStates.value.getActive(), value)
}

const updateProcessDoc = (value) => {
  setProcessDocumentValue(mapStates.value.getActive(), value)
}

const initData = () => {
  isProcess.value = hasProcessRef(mapStates.value.getActive())
  form.value.elementDoc = getDocumentValue(mapStates.value.getActive()) || ''
  if (isProcess.value) {
    form.value.processDoc = getProcessDocumentValue(mapStates.value.getActive()) || ''
  }
}

const eventEmitterListener = () => {
  initData()
}

onBeforeUnmount(() => {
  EventEmitter.removeListener('element-update', eventEmitterListener)
})

onMounted(() => {
  initData()
  EventEmitter.on('element-update', eventEmitterListener)
})
</script>

<template>
  <el-collapse-item name="document">
    <template #title>
      <div class="collapse-header">
        <svg-icon icon="ri:file-word-2-fill"></svg-icon>
        <h2>文档内容</h2>
      </div>
    </template>
    <el-form-item label="元素文档">
      <el-input type="textarea" v-model="form.elementDoc" @change="updateElementDoc" />
    </el-form-item>
    <el-form-item v-if="isProcess" label="流程文档">
      <el-input type="textarea" v-model="form.processDoc" @change="updateProcessDoc" />
    </el-form-item>
  </el-collapse-item>
</template>

<style scoped lang="scss">
.el-collapse-item {
  :deep(.el-collapse-item__header) {

    .collapse-header {
      display: flex;
      flex: 1;
      align-items: center;
      padding: 0 12px;

      .el-icon {
        margin-right: 6px;
        font-size: 20px !important;
      }

      h2 {
        font-weight: bold;
        font-size: 14px;
      }
    }

    .el-icon {
      font-size: 18px;
    }
  }

  :deep(.el-collapse-item__content) {
    padding-right: 12px;
    padding-bottom: 12px;
    padding-left: 12px;
  }
}

.el-form-item {
  :deep(.el-form-item__content) {
    margin-left: 0 !important;
  }
}
</style>
